package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.CourseDao;
import com.woniuxy.dao.ProgressingDao;
import com.woniuxy.entity.Accessory;
import com.woniuxy.entity.Course;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.dto.CourseDto;
import com.woniuxy.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 杜昊男  <br/>
 * Date: 2022/3/7:15:47  <br/>
 * Description:
 */
@Service
public class CoursesServiceImpl implements CourseService {
    @Autowired(required = false)
    CourseDao courseDao;
    @Autowired(required = false)
    ProgressingDao progressingDao;

    @Override
    public ResponseEntity findCourseProgressing(int userid, int courseid) {
        Integer progress = progressingDao.findCourseProgressingDao(userid,courseid);
        if (progress!=null) {
            return ResponseEntity.success("查询成功",progress);
        }else {
            return ResponseEntity.error("查询失败");
        }
    }

    @Override
    public ResponseEntity createCourse(CourseDto courseDto) {
        //附件的id字符串
        String acsIds = "";
        //将附件插入附件表，并返回刚插入数据的id拼接为acsIds字符串
        for (Accessory accessory : courseDto.getAccessoryList()) {
            int res = courseDao.insertAccessory(accessory);
            String accesssoryId = String.valueOf(accessory.getAccessoryid());
            acsIds = accesssoryId + "," + acsIds;
        }
        //拿到courseDto中的Course对象，并将刚刚插入的附件的id封装进去
        Course course = courseDto.getCourse();
        course.setAccessoryid(acsIds);
        int res = courseDao.createCourse(course);
        if (res > 0) {
            return ResponseEntity.success("创建成功！");
        } else {
            return ResponseEntity.error("创建失败");
        }
    }

    @Override
    public ResponseEntity deleteCourse(int id) {
        int res = courseDao.deleteCourse(id);
        if (res > 0) {
            return ResponseEntity.success("删除成功！");
        } else {
            return ResponseEntity.error("删除失败");
        }
    }

    @Override
    public ResponseEntity updateCourse(Course course) {
        int res = courseDao.updateCourse(course);
        if (res > 0) {
            return ResponseEntity.success("删除成功！");
        } else {
            return ResponseEntity.error("删除失败");
        }
    }

    @Override
    public ResponseEntity findCourse(int currentPage, int pageSize, Course course) {
        PageHelper.clearPage();
        PageHelper.startPage(currentPage, pageSize);
        List<Course> courses = courseDao.findCourse(course);
        PageInfo<Course> pf = PageInfo.of(courses);
        if (courses.size() > 0) {
            return ResponseEntity.success("查询成功", pf);
        } else {
            return ResponseEntity.error("查询失败");
        }
    }

    @Override
    public ResponseEntity saveCourseTime(int userid, Course course) {
        Integer progressing = progressingDao.findCourseProgressingDao(userid, course.getId());
        if (progressing != null) {
            if (progressing==100){
                return ResponseEntity.success("进度已完成");
            }
            int res = progressingDao.updateCourseTime(userid,course);
            if (res > 0) {
                return ResponseEntity.success("保存成功");
            }else {
                return ResponseEntity.error("保存失败");
            }
        }else {
            int res = progressingDao.saveCourseTime(userid, course);
            if (res > 0) {
                return ResponseEntity.success("保存成功");
            } else {
                return ResponseEntity.error("保存失败");
            }
        }

    }
}
